import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 模拟多业态分类数据
  const categories = [
    {
      id: 1,
      name: '物理资源',
      code: 'PHYSICAL',
      category_type: 'PHYSICAL',
      parent_id: null,
      level: 1,
      serial_number: '1',
      sort: 1,
      status: 1,
      description: '物理资源分类',
      icon: 'ant-design:database-outlined',
      color: '#1890ff',
      display_config: {},
      tenant_id: 1,
      creator: 'admin',
      create_time: '2024-01-01 00:00:00',
      updater: 'admin',
      update_time: '2024-01-01 00:00:00',
      deleted: 0,
      children: [
        {
          id: 11,
          name: '酒店房间',
          code: 'HOTEL_ROOM',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.1',
          sort: 1,
          status: 1,
          description: '酒店房间资源分类',
          icon: 'ant-design:home-outlined',
          color: '#52c41a',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
        {
          id: 12,
          name: '公寓房间',
          code: 'APARTMENT_ROOM',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.2',
          sort: 2,
          status: 1,
          description: '公寓房间资源分类',
          icon: 'ant-design:apartment-outlined',
          color: '#722ed1',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
        {
          id: 13,
          name: '温泉设施',
          code: 'SPA_FACILITY',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.3',
          sort: 3,
          status: 1,
          description: '温泉洗浴设施资源分类',
          icon: 'ant-design:fire-outlined',
          color: '#fa8c16',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
        {
          id: 14,
          name: '景区设施',
          code: 'SCENIC_FACILITY',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.4',
          sort: 4,
          status: 1,
          description: '景区设施资源分类',
          icon: 'ant-design:environment-outlined',
          color: '#13c2c2',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
        {
          id: 15,
          name: '餐饮设施',
          code: 'RESTAURANT_FACILITY',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.5',
          sort: 5,
          status: 1,
          description: '餐饮设施资源分类',
          icon: 'ant-design:coffee-outlined',
          color: '#eb2f96',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
        {
          id: 16,
          name: '医美设施',
          code: 'MEDICAL_BEAUTY_FACILITY',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.6',
          sort: 6,
          status: 1,
          description: '医美设施资源分类',
          icon: 'ant-design:medicine-box-outlined',
          color: '#f5222d',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
        {
          id: 17,
          name: '美容美发设施',
          code: 'BEAUTY_SALON_FACILITY',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.7',
          sort: 7,
          status: 1,
          description: '美容美发设施资源分类',
          icon: 'ant-design:scissor-outlined',
          color: '#faad14',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
        {
          id: 18,
          name: '设备资源',
          code: 'EQUIPMENT',
          category_type: 'PHYSICAL',
          parent_id: 1,
          level: 2,
          serial_number: '1.8',
          sort: 8,
          status: 1,
          description: '设备资源分类',
          icon: 'ant-design:tool-outlined',
          color: '#faad14',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
      ],
    },
    {
      id: 2,
      name: '人力资源',
      code: 'HUMAN',
      category_type: 'HUMAN',
      parent_id: null,
      level: 1,
      serial_number: '2',
      sort: 2,
      status: 1,
      description: '人力资源分类',
      icon: 'ant-design:user-outlined',
      color: '#52c41a',
      display_config: {},
      tenant_id: 1,
      creator: 'admin',
      create_time: '2024-01-01 00:00:00',
      updater: 'admin',
      update_time: '2024-01-01 00:00:00',
      deleted: 0,
      children: [
        {
          id: 21,
          name: '员工资源',
          code: 'STAFF',
          category_type: 'HUMAN',
          parent_id: 2,
          level: 2,
          serial_number: '2.1',
          sort: 1,
          status: 1,
          description: '员工资源分类',
          icon: 'ant-design:team-outlined',
          color: '#1890ff',
          display_config: {},
          tenant_id: 1,
          creator: 'admin',
          create_time: '2024-01-01 00:00:00',
          updater: 'admin',
          update_time: '2024-01-01 00:00:00',
          deleted: 0,
          children: [],
        },
      ],
    },
    {
      id: 3,
      name: '数字资源',
      code: 'DIGITAL',
      category_type: 'DIGITAL',
      parent_id: null,
      level: 1,
      serial_number: '3',
      sort: 3,
      status: 1,
      description: '数字资源分类',
      icon: 'ant-design:file-outlined',
      color: '#722ed1',
      display_config: {},
      tenant_id: 1,
      creator: 'admin',
      create_time: '2024-01-01 00:00:00',
      updater: 'admin',
      update_time: '2024-01-01 00:00:00',
      deleted: 0,
      children: [],
    },
    {
      id: 4,
      name: '服务资源',
      code: 'SERVICE',
      category_type: 'SERVICE',
      parent_id: null,
      level: 1,
      serial_number: '4',
      sort: 4,
      status: 1,
      description: '服务资源分类',
      icon: 'ant-design:setting-outlined',
      color: '#fa8c16',
      display_config: {},
      tenant_id: 1,
      creator: 'admin',
      create_time: '2024-01-01 00:00:00',
      updater: 'admin',
      update_time: '2024-01-01 00:00:00',
      deleted: 0,
      children: [],
    },
    {
      id: 5,
      name: '业务资源',
      code: 'BUSINESS',
      category_type: 'BUSINESS',
      parent_id: null,
      level: 1,
      serial_number: '5',
      sort: 5,
      status: 1,
      description: '业务资源分类',
      icon: 'ant-design:shop-outlined',
      color: '#eb2f96',
      display_config: {},
      tenant_id: 1,
      creator: 'admin',
      create_time: '2024-01-01 00:00:00',
      updater: 'admin',
      update_time: '2024-01-01 00:00:00',
      deleted: 0,
      children: [],
    },
    {
      id: 6,
      name: '技术资源',
      code: 'TECHNICAL',
      category_type: 'TECHNICAL',
      parent_id: null,
      level: 1,
      serial_number: '6',
      sort: 6,
      status: 1,
      description: '技术资源分类',
      icon: 'ant-design:code-outlined',
      color: '#13c2c2',
      display_config: {},
      tenant_id: 1,
      creator: 'admin',
      create_time: '2024-01-01 00:00:00',
      updater: 'admin',
      update_time: '2024-01-01 00:00:00',
      deleted: 0,
      children: [],
    },
  ];

  // 过滤逻辑
  let filteredCategories = categories;

  if (query.category_type) {
    // 递归过滤，包括子分类
    const filterByType = (cats: any[], type: string): any[] => {
      return cats.filter((cat) => {
        const matchesType = cat.category_type === type;
        const hasMatchingChildren = cat.children && cat.children.length > 0;

        if (hasMatchingChildren) {
          cat.children = filterByType(cat.children, type);
        }

        return matchesType || (hasMatchingChildren && cat.children.length > 0);
      });
    };

    filteredCategories = filterByType(
      filteredCategories,
      query.category_type as string,
    );
  }

  if (query.status !== undefined) {
    const status = Number.parseInt(query.status as string);
    const filterByStatus = (cats: any[]): any[] => {
      return cats.filter((cat) => {
        const matchesStatus = cat.status === status;
        const hasChildren = cat.children && cat.children.length > 0;

        if (hasChildren) {
          cat.children = filterByStatus(cat.children);
        }

        return matchesStatus || (hasChildren && cat.children.length > 0);
      });
    };

    filteredCategories = filterByStatus(filteredCategories);
  }

  if (query.keyword) {
    const keyword = query.keyword as string;
    const filterByKeyword = (cats: any[]): any[] => {
      return cats.filter((cat) => {
        const matchesKeyword =
          cat.name.includes(keyword) || cat.code.includes(keyword);
        const hasChildren = cat.children && cat.children.length > 0;

        if (hasChildren) {
          cat.children = filterByKeyword(cat.children);
        }

        return matchesKeyword || (hasChildren && cat.children.length > 0);
      });
    };

    filteredCategories = filterByKeyword(filteredCategories);
  }

  return {
    code: 0,
    data: {
      items: filteredCategories,
      total: filteredCategories.length,
    },
    msg: '获取分类列表成功',
  };
});
